package ua.races.command.implementation;

import org.apache.log4j.Logger;
import ua.races.command.ServletCommand;
import ua.races.dao.RacesDaoImpl;
import ua.races.relultObjects.FullBet;
import ua.races.vo.Bet;
import ua.races.vo.Client;
import ua.races.vo.Race;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class PrepareClientCommand implements ServletCommand {
    private static final Logger log = Logger.getLogger(PrepareClientCommand.class.getName());

    public void execute(HttpServletRequest req, HttpServletResponse resp, ServletContext context) {

        List<Race> races = null;
        List<Bet> bets = null;
        List<FullBet> fullBets = null;

        RacesDaoImpl dao;

        dao = (RacesDaoImpl) context.getAttribute("DAO");

        try {
            races = dao.findAllRacesForBet();
            bets = dao.findAllClientBets((Client) req.getSession().getAttribute("client"));
            fullBets = dao.getFullBets(bets);
            req.setAttribute("races", races);
            req.setAttribute("fullBets", fullBets);
            log.info("races and bets for client.jsp are successfully prepared");
        } catch (SQLException e) {
            log.error(e);
        }


        try {
            context.getRequestDispatcher("/client.jsp").forward(req, resp);
        } catch (ServletException e) {
            log.error(e);
        } catch (IOException e) {
            log.error(e);
        }

    }
}
